package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.server.base.EmpQuery;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    public Map<String, Object> getAllEmployee(EmpQuery empQuery) {

        //获取查询入职日期范围
        String beginDateScope = empQuery.getBeginDateScope();

        if (beginDateScope != null && beginDateScope != "") {
            System.out.println(beginDateScope);
            String[] split = beginDateScope.split(",");
            empQuery.setBeginDate(split[0]);
            empQuery.setEndDate(split[1]);
        }

        //创建Map对象
        Map<String,Object> map = new HashMap<>();
        //返回的数据类型（来源：官网提供的数据接口）
        map.put("code",0);
        map.put("msg","");

        //开启分页
        PageHelper.startPage(empQuery.getCurrentPage(),empQuery.getSize());
        //获取数据列表
        List<Employee> list = employeeMapper.getAllEmployee(empQuery);

        //得到当前分页对象
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        //总记录数
        map.put("total",pageInfo.getTotal());
        //当前页的列表
        map.put("data",pageInfo.getList());

        return map;
    }


    public List<Position> selectPosition() {
        return employeeMapper.selectPosition();
    }

    public List<Nation> selectNation() {
        return employeeMapper.selectNation();
    }

    public List<PoliticsStatus> selectPoliticsStatus() {
        return employeeMapper.selectPoliticsStatus();
    }

    public List<Joblevel> selectJoblevel() {
        return employeeMapper.selectJoblevel();
    }

    public List<Department> selectDepartment() {
        return employeeMapper.selectDepartment();
    }

    public Map<String, Object> selectWorkId() {
        Map<String,Object> map = new HashMap<>();
        //查询当前数据库最大的员工编号
        String maxWorkID = employeeMapper.selectWorkId();
        //新增员工编号为当前最大员工编号+1
        String newWorkID = String.valueOf(Integer.valueOf(maxWorkID) + 1);
        for (int i = 0; i < maxWorkID.length() - newWorkID.length(); i++) {
            newWorkID = "0" + newWorkID;
        }
        map.put("obj",newWorkID);

        return map;
    }


    @Override
    public boolean updateById(Employee entity) {
        //手机格式判断
        AssertUtil.isTrue(!isMobile(entity.getPhone()),"手机格式不正确！");

        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    public boolean isMobile(String phone) {
        Pattern pt = null;
        Matcher mt = null;
        Boolean flag = false;
        //验证手机号格式
        String str="^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";
        if (null != phone) {
            pt = Pattern.compile(str);
            mt = pt.matcher(phone);
            flag = mt.matches();
        }
        return flag;
    }


}
